package top.z.work.module.system.controller.route;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.z.work.framework.common.pojo.PageInfo;
import top.z.work.framework.common.pojo.R;
import top.z.work.module.system.controller.route.vo.SystemRouteCreateVO;
import top.z.work.module.system.controller.route.vo.SystemRouteModifyVO;
import top.z.work.module.system.controller.route.vo.SystemRouteVO;
import top.z.work.module.system.convert.SystemRouteConvert;
import top.z.work.module.system.entity.SystemRoute;
import top.z.work.module.system.service.SystemRouteService;

import java.util.List;

/**
 * 路由管理Controller
 *
 * @author zaoangod
 * @since 2024-06-24 17:16:14
 */
@Tag(name = "路由管理")
@Slf4j
@Validated
@RestController
@RequestMapping("system/route")
public class SystemRouteController {

    @Resource
    private SystemRouteService systemRouteService;

    @Operation(summary = "路由分页", description = "获取路由分页")
    @GetMapping("page")
    public R<PageInfo<SystemRouteVO>> list(
            SystemRouteVO parameter,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "size", defaultValue = "10") Integer size
    ) {
        QueryWrapper      query    = SystemRouteConvert.INSTANCE.convert(parameter);
        Page<SystemRoute> data     = Page.of(page, size);
        Page<SystemRoute> pageInfo = systemRouteService.page(data, query);
        return R.s(SystemRouteConvert.INSTANCE.convert(pageInfo));
    }

    @Operation(summary = "路由列表", description = "获取路由列表")
    @GetMapping("list")
    public R<List<SystemRouteVO>> list(SystemRouteVO parameter) {
        QueryWrapper      query = SystemRouteConvert.INSTANCE.convert(parameter);
        List<SystemRoute> list  = systemRouteService.list(query);
        return R.s(SystemRouteConvert.INSTANCE.convert(list));
    }

    @Operation(summary = "路由信息", description = "获取路由信息")
    @GetMapping("info")
    public R<SystemRouteVO> queryOne(SystemRouteVO parameter) {
        QueryWrapper query       = SystemRouteConvert.INSTANCE.convert(parameter);
        SystemRoute  SystemRoute = systemRouteService.getOne(query);
        return R.s(SystemRouteConvert.INSTANCE.convert(SystemRoute));
    }

    @Operation(summary = "删除路由", description = "删除路由数据")
    @DeleteMapping("remove")
    public R<Boolean> remove(@RequestParam("identity") @NotNull(message = "数据主键不能为空") Long identity) {
        boolean success = systemRouteService.removeById(identity);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "修改路由", description = "修改路由数据")
    @PutMapping("modify")
    public R<Boolean> modify(@RequestBody SystemRouteModifyVO parameter) {
        SystemRoute dept    = SystemRouteConvert.INSTANCE.convert(parameter);
        boolean     success = systemRouteService.updateById(dept);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "添加路由", description = "添加路由数据")
    @PostMapping("add")
    public R<Boolean> add(@RequestBody SystemRouteCreateVO parameter) {
        SystemRoute dept    = SystemRouteConvert.INSTANCE.convert(parameter);
        boolean     success = systemRouteService.save(dept);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "批量删除", description = "批量删除配置数据")
    @PostMapping("batch-remove")
    public R<Boolean> batchRemove(
            @RequestBody
            @NotNull(message = "数据主键列表不能为空")
            @NotEmpty(message = "数据主键列表不能为空")
            List<String> identityList
    ) {
        boolean success = systemRouteService.removeByIds(identityList);
        return success ? R.s(true) : R.f();
    }

}
